/*

	Paper tables
		Makes the tables for the Peers and Motivation at Work paper

*/

*Set directory here at the top
global dir Z:/Dropbox/SPE/Writeups/Submissions


global JHRdir ${dir}/JHR/Data
global bayes_results ${JHRdir}/BayesSEs
capture mkdir "${bayes_results}"
global tables ${JHRdir}/Raw_Tables
capture mkdir "${tables}"


*pull in data

use "${JHRdir}/bayes_data_all.dta", clear
*vector of ones as "FE" for regs with no FEs
gen ones = 1



	
capture program drop make_bayes_SEs
program define make_bayes_SEs
syntax, tablename(string) outcome(string) regressors(string) absorb(string) cluster(string) sample(string) draws(string)
	
	quietly{
		
		*restrict sample appropriately
			local ifstatement
			if "`sample'"=="all"{
				local ifstatement
			}
			else if "`sample'"=="noday1"{
				local ifstatement if day_of_cycle!=1
			}
			else if "`sample'"=="justday1"{
				local ifstatement if day_of_cycle==1
			}
			else if "`sample'"=="noday1alt"{
				local ifstatement if day_of_cycle_alt!=1
			}			
			else if "`sample'"=="noday1female"{
				local ifstatement if day_of_cycle!=1 & female
			}	
			
			else if "`sample'"=="noday1nofriends"{
				local ifstatement if day_of_cycle!=1 & fr123_neighbor_ability_any==0
			}	
			
			else if "`sample'"=="noday1withfriends"{
				local ifstatement if day_of_cycle!=1 & fr123_neighbor_ability_any==1
			}			
			

		*run main regs
			preserve
				*draw number "zero" has the original data
				keep if draw==0
				reghdfe `outcome' `regressors', absorb(`absorb') cluster(`cluster'), `ifstatement'
				
				local df_r = e(df_r)
				
				matrix b=e(b)
				*get number of regressors
				local n_regressors = colsof(b)
				matrix V=e(V)
				
				forvalues i = 1/`n_regressors'{

					local b_`i' = b[1,`i']
					local se_`i' = sqrt(V[`i',`i'])
					
				}
			restore
			
		*make dataset to store output
			preserve
				*one obs per draw plus the main results
				drop draw
				gen int draw = _n-1
				keep if draw<=`draws'
				keep draw
				forvalues i = 1/`n_regressors'{
					gen double b_`i' = .
					gen double se_`i' = .
				}
				tempfile bayes_estimates
				save `bayes_estimates', replace
				
			restore
					
		
		*run bayes SE regs
			forvalues draw=1/`draws'{
			
				preserve
					*get just this draw of the data draw number "zero" has the original data
					keep if draw==`draw'
					
					reghdfe `outcome' `regressors', absorb(`absorb') cluster(`cluster'), `ifstatement'
					matrix b=e(b)
					forvalues i = 1/`n_regressors'{

						local b_`i'_`draw' = b[1,`i']
						
					}
				restore
			
			
			}
			
		*store results in our little dataset
			preserve
			
				use `bayes_estimates', clear
				
				*get results for all regressors
				forvalues i = 1/`n_regressors'{
					*main results
					replace b_`i' = `b_`i'' if draw == 0
					replace se_`i' = `se_`i'' if draw == 0
					
					*bayes results
						forvalues draw=1/`draws'{
							replace b_`i' = `b_`i'_`draw'' if draw==`draw'
						}			
					
					save `bayes_estimates', replace
				}
				
			restore
			
		*save SEs in results folder
			preserve
				use `bayes_estimates', clear	
				forvalues i = 1/`n_regressors'{
					local b_`i' = b_`i'[1]
					local se_`i' = se_`i'[1]
					local asym_var_`i'=(`se_`i'')^2
				}
				
				keep if draw>=1
				drop se*
				
				*get var of simulation betas and build total SE
				forvalues i = 1/`n_regressors'{
					sum b_`i'
					local sim_var_`i' = r(Var)
					local bayes_se_`i' = sqrt(`asym_var_`i'' + `sim_var_`i'')			
				}
				
				
				*output results
				keep if draw == 1
				keep draw
				forvalues i = 1/`n_regressors'{
					gen double b_`i' = `b_`i''
					gen double bayes_se_`i' = `bayes_se_`i''
				}		
				drop draw
							
				
		
				*convert to long form and format numbers
					gen i = 1
					reshape long b_ bayes_se_, i(i) j(var)
					drop i
					rename b_ b
					rename bayes_se_ bayes_se
					
					gen p = 2*(1-t(`df_r',abs(b)/bayes_se))
					gen stars = ""
					replace stars = "*" if p<0.1
					replace stars = "**" if p<0.05
					replace stars = "***" if p<0.01
					replace b = round(b,0.001)
					replace bayes_se = round(bayes_se,0.001)
					tostring *, replace force
					replace b = "-0"+substr(b,2,7)+"000" if substr(b,1,1)=="-"
					replace b = "0"+b+"000" if substr(b,1,1)!="-"
					replace b = substr(b,1,6)+stars if substr(b,1,1)=="-"
					replace b = substr(b,1,5)+stars if substr(b,1,1)!="-"
					replace bayes_se = "0"+bayes_se+"000"
					replace bayes_se  = substr(bayes_se,1,5)
					replace bayes_se = "(" + bayes_se +")"
				
				tempfile col
				save `col', replace
				
				*put vars in right order in a single column
					keep var b
					rename b estimate
					gen var_fancy = var+" (beta)"
					gen order = 1
					
					tempfile b
					save `b', replace
					
					use `col', clear
					
					keep var bayes_se
					rename bayes_se estimate
					gen var_fancy = var+" (bayes_se)"
					
					gen order = 2
					
					tempfile bayes_se
					save `bayes_se', replace
					
					append using `b'
					sort var order
					keep var_fancy estimate
					order var_fancy estimate	
					
				*save results
					save "${bayes_results}/`tablename' absorb(`absorb') cluster(`cluster') sample(`sample') draws(`draws').dta", replace
					export excel "${bayes_results}/`tablename' absorb(`absorb') cluster(`cluster') sample(`sample') draws(`draws').xlsx", replace
				
			restore
			
	}
	*end block to do quietly
end



	
*Paper Tables

	*T1 
		*Summary statistics

	*T2
	*balance
		reghdfe ln_own_ability ln_mean_peer_ability ln_loo_mean_peer_ability, a(ones) cluster(cycle_day_id dt_loc_num), if day_of_cycle==1 & draw ==0
		outreg2 ln_mean_peer_ability ln_loo_mean_peer_ability using "${tables}/Table2.xls", adjr2 dec(3) fmt(fc) nocons excel ///
			keep(ln_mean_peer_ability ln_loo_mean_peer_ability) sortvar(ln_mean_peer_ability ln_loo_mean_peer_ability) replace
		make_bayes_SEs, tablename(T2 col1) outcome(ln_own_ability) regressors(ln_mean_peer_ability ln_loo_mean_peer_ability) absorb(ones) cluster(cycle_day_id dt_loc_num) sample(justday1) draws(10)
		
		
		reghdfe ln_own_ability ln_mean_peer_ability ln_loo_mean_peer_ability, a(ones) cluster(cycle_day_id dt_loc_num), if draw ==0
		outreg2 ln_mean_peer_ability ln_loo_mean_peer_ability using "${tables}/Table2.xls", adjr2 dec(3) fmt(fc) nocons excel ///
			keep(ln_mean_peer_ability ln_loo_mean_peer_ability) sortvar(ln_mean_peer_ability ln_loo_mean_peer_ability) append
		make_bayes_SEs, tablename(T2 col2) outcome(ln_own_ability) regressors(ln_mean_peer_ability ln_loo_mean_peer_ability) absorb(ones) cluster(cycle_day_id dt_loc_num) sample(all) draws(10)

		
		reghdfe ln_own_ability ln_mean_peer_ability ln_loo_mean_peer_ability, a(ones) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw ==0
		outreg2 ln_mean_peer_ability ln_loo_mean_peer_ability using "${tables}/Table2.xls", adjr2 dec(3) fmt(fc) nocons excel ///
			keep(ln_mean_peer_ability ln_loo_mean_peer_ability) sortvar(ln_mean_peer_ability ln_loo_mean_peer_ability) append
		make_bayes_SEs, tablename(T2 col3) outcome(ln_own_ability) regressors(ln_mean_peer_ability ln_loo_mean_peer_ability) absorb(ones) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)


		reghdfe ln_own_ability ln_mean_peer_ability ln_loo_mean_peer_ability, a(dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw ==0
		outreg2 ln_mean_peer_ability ln_loo_mean_peer_ability using "${tables}/Table2.xls", adjr2 dec(3) fmt(fc) nocons excel ///
			keep(ln_mean_peer_ability ln_loo_mean_peer_ability) sortvar(ln_mean_peer_ability ln_loo_mean_peer_ability) append
		make_bayes_SEs, tablename(T2 col4) outcome(ln_own_ability) regressors(ln_mean_peer_ability ln_loo_mean_peer_ability) absorb(dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)
			

	
	*T3
	*main peer effects estimates
		reghdfe ln_kg ln_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
		outreg2 ln_mean_peer_ability using "${tables}/Table3.xls", adjr2 dec(3) fmt(fc) nocons excel ///
			keep(ln_mean_peer_ability) sortvar(ln_mean_peer_ability) replace
		make_bayes_SEs, tablename(T3 col1) outcome(ln_kg) regressors(ln_mean_peer_ability) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)
			
		
		reghdfe ln_kg ln_mean_peer_ability, a(id Date Location) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
		outreg2 ln_mean_peer_ability using "${tables}/Table3.xls", adjr2 dec(3) fmt(fc) nocons excel ///
			keep(ln_mean_peer_ability) sortvar(ln_mean_peer_ability) append
		make_bayes_SEs, tablename(T3 col2) outcome(ln_kg) regressors(ln_mean_peer_ability) absorb(id Date Location) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)
	


	*T4
	*heterogeneity by worker characteristics
		reghdfe ln_kg i.female#c.ln_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
		outreg2 i.female#c.ln_mean_peer_ability using "${tables}/Table4.xls", adjr2 dec(3) fmt(fc) nocons excel ///
			keep(i.female#c.ln_mean_peer_ability) replace
			*our effects are all for women
			
			lincom _b[1.female#c.ln_mean_peer_ability]-_b[0.female#c.ln_mean_peer_ability]				
		make_bayes_SEs, tablename(T4 col1) outcome(ln_kg) regressors(i.female#c.ln_mean_peer_ability) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)
		
		reghdfe ln_kg i.age_quartile#c.ln_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
		outreg2 i.age_quartile#c.ln_mean_peer_ability using "${tables}/Table4.xls", adjr2 dec(3) fmt(fc) nocons excel ///
			keep(i.age_quartile#c.ln_mean_peer_ability) append
			*stronger effects for youngest people too
			
			lincom _b[1.age_quartile#c.ln_mean_peer_ability]-_b[2.age_quartile#c.ln_mean_peer_ability]
			lincom _b[1.age_quartile#c.ln_mean_peer_ability]-_b[3.age_quartile#c.ln_mean_peer_ability]
			lincom _b[1.age_quartile#c.ln_mean_peer_ability]-_b[4.age_quartile#c.ln_mean_peer_ability]
		make_bayes_SEs, tablename(T4 col2) outcome(ln_kg) regressors(i.age_quartile#c.ln_mean_peer_ability) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)
			
			
		reghdfe ln_kg i.constant_ability_quartile#c.ln_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
		outreg2 i.constant_ability_quartile#c.ln_mean_peer_ability using "${tables}/Table4.xls", adjr2 dec(3) fmt(fc) nocons excel ///
			keep(i.constant_ability_quartile#c.ln_mean_peer_ability) append
		make_bayes_SEs, tablename(T4 col3) outcome(ln_kg) regressors(i.constant_ability_quartile#c.ln_mean_peer_ability) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		
		
		
	*T5
	*friend vs nonfriend analysis
		reghdfe ln_kg zln_fr0_neighbor_ability_avg fr0_neighbor_ability_none, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
		outreg2 zln_fr0_neighbor_ability_avg fr0_neighbor_ability_none using "${tables}/Table5.xls", adjr2 dec(3) fmt(fc) nocons excel ///
			keep(zln_fr0_neighbor_ability_avg fr0_neighbor_ability_none) replace
		make_bayes_SEs, tablename(T5 col1) outcome(ln_kg) regressors(zln_fr0_neighbor_ability_avg fr0_neighbor_ability_none) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		

		reghdfe ln_kg zln_fr123_neighbor_ability_avg fr123_neighbor_ability_none, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
		outreg2 zln_fr123_neighbor_ability_avg fr123_neighbor_ability_none using "${tables}/Table5.xls", adjr2 dec(3) fmt(fc) nocons excel ///
			keep(zln_fr123_neighbor_ability_avg fr123_neighbor_ability_none) append
		make_bayes_SEs, tablename(T5 col2) outcome(ln_kg) regressors(zln_fr123_neighbor_ability_avg fr123_neighbor_ability_none) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		
		
		reghdfe ln_kg zln_fr0_neighbor_ability_avg fr0_neighbor_ability_none zln_fr123_neighbor_ability_avg fr123_neighbor_ability_none, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
		outreg2 zln_fr0_neighbor_ability_avg fr0_neighbor_ability_none zln_fr123_neighbor_ability_avg fr123_neighbor_ability_none using "${tables}/Table5.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(zln_fr0_neighbor_ability_avg fr0_neighbor_ability_none zln_fr123_neighbor_ability_avg fr123_neighbor_ability_none) sortvar(zln_fr0_neighbor_ability_avg fr0_neighbor_ability_none zln_fr123_neighbor_ability_avg fr123_neighbor_ability_none) append
	
		lincom _b[zln_fr0_neighbor_ability_avg] -_b[zln_fr123_neighbor_ability_avg]
		
			*log mean ability for a non-friend
			di ln(61.28)

			*log mean ability for a friend
			di ln(64.23)

		lincom   (_b[zln_fr0_neighbor_ability_avg]*4.1154535 - _b[fr0_neighbor_ability_none] ) ///
				-(_b[zln_fr123_neighbor_ability_avg]*4.1624704 - _b[fr123_neighbor_ability_none]) 
				
		make_bayes_SEs, tablename(T5 col3) outcome(ln_kg) regressors(zln_fr0_neighbor_ability_avg fr0_neighbor_ability_none zln_fr123_neighbor_ability_avg fr123_neighbor_ability_none) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		
		
		reghdfe ln_kg ln_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0 & fr123_neighbor_ability_none ==0
		outreg2 ln_mean_peer_ability using "${tables}/Table5.xls", adjr2 dec(3) fmt(fc) nocons excel ///
			keep(ln_mean_peer_ability) append
		make_bayes_SEs, tablename(T5 col4) outcome(ln_kg) regressors(ln_mean_peer_ability) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1nofriends) draws(10)		
	
		reghdfe ln_kg ln_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0 & fr123_neighbor_ability_none ==1
		outreg2 ln_mean_peer_ability using "${tables}/Table5.xls", adjr2 dec(3) fmt(fc) nocons excel ///
			keep(ln_mean_peer_ability) append
		make_bayes_SEs, tablename(T5 col5) outcome(ln_kg) regressors(ln_mean_peer_ability) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1withfriends) draws(10)	
		

	*T6
	*results by experience level
		reghdfe ln_kg c.ln_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
		outreg2 c.ln_mean_peer_ability using "${tables}/Table6.xls", adjr2 dec(3) fmt(fc) nocons excel ///
			keep(c.ln_mean_peer_ability) sortvar(c.ln_mean_peer_ability) replace
		make_bayes_SEs, tablename(T6 col1) outcome(ln_kg) regressors(c.ln_mean_peer_ability) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		

		reghdfe ln_kg i.new_worker##c.ln_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
		outreg2 i.new_worker##c.ln_mean_peer_ability using "${tables}/Table6.xls", adjr2 dec(3) fmt(fc) nocons excel ///
			keep(i.new_worker##c.ln_mean_peer_ability) append
		make_bayes_SEs, tablename(T6 col2) outcome(ln_kg) regressors(i.new_worker##c.ln_mean_peer_ability) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		
		
		reghdfe ln_kg i.experience_quartile#c.ln_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
		test 1.experience_quartile#c.ln_mean_peer_ability=2.experience_quartile#c.ln_mean_peer_ability=3.experience_quartile#c.ln_mean_peer_ability=4.experience_quartile#c.ln_mean_peer_ability
		outreg2 i.experience_quartile#c.ln_mean_peer_ability using "${tables}/Table6.xls", adjr2 dec(3) fmt(fc) nocons excel ///
			keep(i.experience_quartile#c.ln_mean_peer_ability) append
			
		make_bayes_SEs, tablename(T6 col3) outcome(ln_kg) regressors(i.experience_quartile#c.ln_mean_peer_ability) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		
				
				
	*T7
	*results for lagged peer ability
	
			reghdfe ln_kg ln_mean_peer_ability lag1_ln_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
			outreg2 ln_mean_peer_ability lag1_ln_mean_peer_ability using "${tables}/Table7.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(ln_mean_peer_ability lag1_ln_mean_peer_ability) sortvar(ln_mean_peer_ability lag1_ln_mean_peer_ability) replace
			make_bayes_SEs, tablename(T7 col1) outcome(ln_kg) regressors(ln_mean_peer_ability lag1_ln_mean_peer_ability) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		

					
			reghdfe ln_kg ln_mean_peer_ability lag1_ln_mean_peer_ability lag2_ln_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
			outreg2 ln_mean_peer_ability lag1_ln_mean_peer_ability lag2_ln_mean_peer_ability using "${tables}/Table7.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(ln_mean_peer_ability lag1_ln_mean_peer_ability lag2_ln_mean_peer_ability) sortvar(ln_mean_peer_ability lag1_ln_mean_peer_ability lag2_ln_mean_peer_ability) append
			make_bayes_SEs, tablename(T7 col2) outcome(ln_kg) regressors(ln_mean_peer_ability lag1_ln_mean_peer_ability lag2_ln_mean_peer_ability) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		
			
			reghdfe ln_kg ln_mean_peer_ability lag1_ln_mean_peer_ability lag2_ln_mean_peer_ability lag3_ln_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
			outreg2 ln_mean_peer_ability lag1_ln_mean_peer_ability lag2_ln_mean_peer_ability lag3_ln_mean_peer_ability using "${tables}/Table7.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(ln_mean_peer_ability lag1_ln_mean_peer_ability lag2_ln_mean_peer_ability lag3_ln_mean_peer_ability) sortvar(ln_mean_peer_ability lag1_ln_mean_peer_ability lag2_ln_mean_peer_ability lag3_ln_mean_peer_ability) append
			make_bayes_SEs, tablename(T7 col3) outcome(ln_kg) regressors(ln_mean_peer_ability lag1_ln_mean_peer_ability lag2_ln_mean_peer_ability lag3_ln_mean_peer_ability) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		

			reghdfe ln_kg lag1_ln_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0	
			outreg2 lag1_ln_mean_peer_ability using "${tables}/Table7.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(lag1_ln_mean_peer_ability) append
			make_bayes_SEs, tablename(T7 col4) outcome(ln_kg) regressors(lag1_ln_mean_peer_ability) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		


			reghdfe ln_kg lag1_ln_mean_peer_ability lag2_ln_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0	
			outreg2 lag1_ln_mean_peer_ability lag2_ln_mean_peer_ability using "${tables}/Table7.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(lag1_ln_mean_peer_ability lag2_ln_mean_peer_ability) append
			make_bayes_SEs, tablename(T7 col5) outcome(ln_kg) regressors(lag1_ln_mean_peer_ability lag2_ln_mean_peer_ability) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		

			reghdfe ln_kg lag1_ln_mean_peer_ability lag2_ln_mean_peer_ability lag3_ln_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0	
			outreg2 lag1_ln_mean_peer_ability lag2_ln_mean_peer_ability lag3_ln_mean_peer_ability using "${tables}/Table7.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(lag1_ln_mean_peer_ability lag2_ln_mean_peer_ability lag3_ln_mean_peer_ability) append
			make_bayes_SEs, tablename(T7 col6) outcome(ln_kg) regressors(lag1_ln_mean_peer_ability lag2_ln_mean_peer_ability lag3_ln_mean_peer_ability) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		

	*T8
	*preferences
	
		*bring in WTP data
			
		preserve
		
			use "${JHRdir}/wtp_data.dta", clear
			
			
			
					
			*table 8
				tab wtppick_any
				
				tab soap1_fmod_uncond
				tab soap2_fmod_uncond
				
			*appendix table 12
				tab wtppick_any female, col nofreq
				
				gen fast = wtppick_any ==1 if !mi(wtppick_any)
				gen slow = wtppick_any ==2 if !mi(wtppick_any)
				gen specific = wtppick_any ==3 if !mi(wtppick_any)
				gen none = wtppick_any ==4 if !mi(wtppick_any)
				
				reg fast female, r
				outreg2 female using "${tables}/AppendixTable12.xls", adjr2 dec(3) fmt(fc) nocons excel keep(female) replace
				reg fast female age married hhsize_equiv exp_pc exp, r
				outreg2 female using "${tables}/AppendixTable12.xls", adjr2 dec(3) fmt(fc) nocons excel keep(female) append
				reg slow female, r
				outreg2 female using "${tables}/AppendixTable12.xls", adjr2 dec(3) fmt(fc) nocons excel keep(female) append
				reg slow female age married hhsize_equiv exp_pc exp, r
				outreg2 female using "${tables}/AppendixTable12.xls", adjr2 dec(3) fmt(fc) nocons excel keep(female) append			
				reg specific female, r
				outreg2 female using "${tables}/AppendixTable12.xls", adjr2 dec(3) fmt(fc) nocons excel keep(female) append
				reg specific female age married hhsize_equiv exp_pc exp, r
				outreg2 female using "${tables}/AppendixTable12.xls", adjr2 dec(3) fmt(fc) nocons excel keep(female) append			
				reg none female, r
				outreg2 female using "${tables}/AppendixTable12.xls", adjr2 dec(3) fmt(fc) nocons excel keep(female) append
				reg none female age married hhsize_equiv exp_pc exp, r
				outreg2 female using "${tables}/AppendixTable12.xls", adjr2 dec(3) fmt(fc) nocons excel keep(female) append			
				
				tab soap1_fmod_uncond female, col nofreq,
				
				reg soap1_fmod_uncond female, r,		
				outreg2 female using "${tables}/AppendixTable12.xls", adjr2 dec(3) fmt(fc) nocons excel keep(female) append
				
				reg soap1_fmod_uncond female age married hhsize_equiv exp_pc exp, r,		
				outreg2 female using "${tables}/AppendixTable12.xls", adjr2 dec(3) fmt(fc) nocons excel keep(female) append
							
				
				tab soap2_fmod_uncond female, col nofreq,				
				
				reg soap2_fmod_uncond female, r,	
				outreg2 female using "${tables}/AppendixTable12.xls", adjr2 dec(3) fmt(fc) nocons excel keep(female) append
				
				reg soap2_fmod_uncond female age married hhsize_equiv exp_pc exp, r,	
				outreg2 female using "${tables}/AppendixTable12.xls", adjr2 dec(3) fmt(fc) nocons excel keep(female) append
							
				
				tab wtpfast_afterallfast_why female, col nofreq
				 // statistic for text

		restore			

*Appendix Tables

	*Appendix T1
		*regression of worker ability on worker characteristics
		preserve
			keep if draw==0
			keep id constant_own_ability age female married hhsize_equiv spending_per_capita experience ///
				experience_quartile new_worker age_quartile employmkg_carry_tea employmkg_carry_maize ///
				employmdays_help_carrykg strength_quartile alt_strength_quartile 
				
			
			drop if mi(constant_own_ability,age,female,married,hhsize_equiv,spending_per_capita,experience,new_worker)
			duplicates drop
			
			reg constant_own_ability female married hhsize_equiv spending_per_capita age experience, r
			outreg2 female married hhsize_equiv spending_per_capita age experience using "${tables}/AppendixTable1.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(female married hhsize_equiv spending_per_capita age experience) replace
				
			reg constant_own_ability female married hhsize_equiv spending_per_capita i.age_quartile i.experience_quartile, r
			outreg2 female married hhsize_equiv spending_per_capita i.age_quartile i.experience_quartile using "${tables}/AppendixTable1.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(female married hhsize_equiv spending_per_capita i.age_quartile i.experience_quartile) sortvar(female married hhsize_equiv spending_per_capita age i.age_quartile experience i.experience_quartile) append
			
			reg constant_own_ability female married hhsize_equiv spending_per_capita age experience employmkg_carry_tea, r
			
			reg constant_own_ability female married hhsize_equiv spending_per_capita age experience i.strength_quartile, r
			
			reg constant_own_ability female married hhsize_equiv spending_per_capita age experience i.alt_strength_quartile i.strength_quartile, r

			reg constant_own_ability female married hhsize_equiv spending_per_capita age experience employmkg_carry_tea employmkg_carry_maize, r
			
		restore
		
		
	*Appendix T2
		*additional balance checks

			reghdfe age ln_mean_peer_ability ln_loo_mean_peer_ability, ///
				a(dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw ==0
			outreg2 ln_mean_peer_ability ln_loo_mean_peer_ability using "${tables}/AppendixTable2.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(ln_mean_peer_ability ln_loo_mean_peer_ability ) replace	
			make_bayes_SEs, tablename(AT2 col1) outcome(age) regressors(ln_mean_peer_ability ln_loo_mean_peer_ability) absorb(dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		
				
			reghdfe female ln_mean_peer_ability ln_loo_mean_peer_ability, ///
				a(dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw ==0
			outreg2 ln_mean_peer_ability ln_loo_mean_peer_ability using "${tables}/AppendixTable2.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(ln_mean_peer_ability ln_loo_mean_peer_ability ) append	
			make_bayes_SEs, tablename(AT2 col2) outcome(female) regressors(ln_mean_peer_ability ln_loo_mean_peer_ability) absorb(dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		

			reghdfe married ln_mean_peer_ability ln_loo_mean_peer_ability, ///
				a(dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw ==0
			outreg2 ln_mean_peer_ability ln_loo_mean_peer_ability using "${tables}/AppendixTable2.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(ln_mean_peer_ability ln_loo_mean_peer_ability ) append	
			make_bayes_SEs, tablename(AT2 col3) outcome(married) regressors(ln_mean_peer_ability ln_loo_mean_peer_ability) absorb(dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		

			reghdfe experience ln_mean_peer_ability ln_loo_mean_peer_ability, ///
				a(dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw ==0
			outreg2 ln_mean_peer_ability ln_loo_mean_peer_ability using "${tables}/AppendixTable2.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(ln_mean_peer_ability ln_loo_mean_peer_ability ) append	
			make_bayes_SEs, tablename(AT2 col4) outcome(experience) regressors(ln_mean_peer_ability ln_loo_mean_peer_ability) absorb(dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		

			reghdfe hhsize_equiv ln_mean_peer_ability ln_loo_mean_peer_ability, ///
				a(dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw ==0
			outreg2 ln_mean_peer_ability ln_loo_mean_peer_ability using "${tables}/AppendixTable2.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(ln_mean_peer_ability ln_loo_mean_peer_ability ) append	
			make_bayes_SEs, tablename(AT2 col5) outcome(hhsize_equiv) regressors(ln_mean_peer_ability ln_loo_mean_peer_ability) absorb(dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		

			reghdfe new_worker ln_mean_peer_ability ln_loo_mean_peer_ability, ///
				a(dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw ==0
			outreg2 ln_mean_peer_ability ln_loo_mean_peer_ability using "${tables}/AppendixTable2.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(ln_mean_peer_ability ln_loo_mean_peer_ability ) append	
			make_bayes_SEs, tablename(AT2 col6) outcome(new_worker) regressors(ln_mean_peer_ability ln_loo_mean_peer_ability) absorb(dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		

			reghdfe spending_per_capita ln_mean_peer_ability ln_loo_mean_peer_ability, ///
				a(dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw ==0
			outreg2 ln_mean_peer_ability ln_loo_mean_peer_ability using "${tables}/AppendixTable2.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(ln_mean_peer_ability ln_loo_mean_peer_ability ) append	
			make_bayes_SEs, tablename(AT2 col7) outcome(spending_per_capita) regressors(ln_mean_peer_ability ln_loo_mean_peer_ability) absorb(dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		

	*Appendix T3
	*sorting of workers to friends on non-random assignment days
		capture drop cycle_day_1
		gen cycle_day_1 = day_of_cycle==1 if !mi(day_of_cycle)
		reg fr123_neighbor_ability_any cycle_day_1, cluster(id), if draw==0	
		outreg2 cycle_day_1 using "${tables}/AppendixTable3.xls", adjr2 dec(3) fmt(fc) nocons excel ///
			keep(cycle_day_1) replace	
		
		sum fr123_neighbor_ability_any if e(sample) & day_of_cycle != 1
		sum fr123_neighbor_ability_any if e(sample) & day_of_cycle == 1
		
		reghdfe fr123_neighbor_ability_any cycle_day_1, a(id) cluster(id), if draw==0	
		outreg2 cycle_day_1 using "${tables}/AppendixTable3.xls", adjr2 dec(3) fmt(fc) nocons excel ///
			keep(cycle_day_1) append	
		
		sum fr123_neighbor_ability_any if e(sample) & day_of_cycle != 1
		sum fr123_neighbor_ability_any if e(sample) & day_of_cycle == 1		
		
		

	*Appendix T4
	*add controls for other peer characteristics

		
			reghdfe ln_kg ln_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
			outreg2 ln_mean_peer_ability using "${tables}/AppendixTable4.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(ln_mean_peer_ability) replace
			make_bayes_SEs, tablename(AT3 col1) outcome(ln_kg) regressors(ln_mean_peer_ability) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		
				
			reghdfe ln_kg ln_mean_peer_ability n_neighbors, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
			outreg2 ln_mean_peer_ability n_neighbors using "${tables}/AppendixTable4.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(ln_mean_peer_ability n_neighbors) append				
			make_bayes_SEs, tablename(AT3 col2) outcome(ln_kg) regressors(ln_mean_peer_ability n_neighbors) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		

			reghdfe ln_kg ln_mean_peer_ability pAvg_age, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
			outreg2 ln_mean_peer_ability pAvg_age using "${tables}/AppendixTable4.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(ln_mean_peer_ability pAvg_age) append	
			make_bayes_SEs, tablename(AT3 col3) outcome(ln_kg) regressors(ln_mean_peer_ability pAvg_age) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		
				
			reghdfe ln_kg ln_mean_peer_ability pAvg_female, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
			outreg2 ln_mean_peer_ability pAvg_female using "${tables}/AppendixTable4.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(ln_mean_peer_ability pAvg_female) append	
			make_bayes_SEs, tablename(AT3 col4) outcome(ln_kg) regressors(ln_mean_peer_ability pAvg_female) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		
				
			reghdfe ln_kg ln_mean_peer_ability pAvg_married, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
			outreg2 ln_mean_peer_ability pAvg_married using "${tables}/AppendixTable4.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(ln_mean_peer_ability pAvg_married) append	
			make_bayes_SEs, tablename(AT3 col5) outcome(ln_kg) regressors(ln_mean_peer_ability pAvg_married) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		
				
			reghdfe ln_kg ln_mean_peer_ability pAvg_hhsize_equiv, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
			outreg2 ln_mean_peer_ability pAvg_hhsize_equiv using "${tables}/AppendixTable4.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(ln_mean_peer_ability pAvg_hhsize_equiv) append	
			make_bayes_SEs, tablename(AT3 col6) outcome(ln_kg) regressors(ln_mean_peer_ability pAvg_hhsize_equiv) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		
				
			reghdfe ln_kg ln_mean_peer_ability pAvg_experience, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
			outreg2 ln_mean_peer_ability pAvg_experience using "${tables}/AppendixTable4.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(ln_mean_peer_ability pAvg_experience) append	
			make_bayes_SEs, tablename(AT3 col7) outcome(ln_kg) regressors(ln_mean_peer_ability pAvg_experience) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		
				
			reghdfe ln_kg ln_mean_peer_ability pAvg_sameSex, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
			outreg2 ln_mean_peer_ability pAvg_sameSex using "${tables}/AppendixTable4.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(ln_mean_peer_ability pAvg_sameSex) append	
			make_bayes_SEs, tablename(AT3 col8) outcome(ln_kg) regressors(ln_mean_peer_ability pAvg_sameSex) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		
				
			reghdfe ln_kg ln_mean_peer_ability n_neighbors pAvg_age pAvg_female pAvg_married pAvg_hhsize_equiv pAvg_experience pAvg_sameSex, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
			outreg2 ln_mean_peer_ability n_neighbors pAvg_age pAvg_female pAvg_married pAvg_hhsize_equiv pAvg_experience pAvg_sameSex using "${tables}/AppendixTable4.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(ln_mean_peer_ability n_neighbors pAvg_age pAvg_female pAvg_married pAvg_hhsize_equiv pAvg_experience pAvg_sameSex) append	
			make_bayes_SEs, tablename(AT3 col9) outcome(ln_kg) regressors(ln_mean_peer_ability n_neighbors pAvg_age pAvg_female pAvg_married pAvg_hhsize_equiv pAvg_experience pAvg_sameSex) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		

				
			*additional results for looking at gender impacts
			reghdfe ln_kg pAvg_male, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0 & female
			di 2.8*_se[pAvg_male]
			make_bayes_SEs, tablename(AT3 colAdditional) outcome(ln_kg) regressors(pAvg_male) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1female) draws(10)		
			
	*Appendix T5
	*second-order peer effects estimates
		preserve
		
			use "${JHRdir}/2nd_order_nbrs.dta", clear
			reghdfe ln_kg ln_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num)
			outreg2 ln_mean_peer_ability using "${tables}/AppendixTable5.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(ln_mean_peer_ability) sortvar(ln_mean_peer_ability) replace
				
			reghdfe ln_kg ln_mean_2ndorder_nbr_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num)
			outreg2 ln_mean_2ndorder_nbr_ability using "${tables}/AppendixTable5.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(ln_mean_2ndorder_nbr_ability) sortvar(ln_mean_2ndorder_nbr_ability) append			
				
			reghdfe ln_kg ln_mean_peer_ability ln_mean_2ndorder_nbr_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num)
			outreg2 ln_mean_peer_ability ln_mean_2ndorder_nbr_ability using "${tables}/AppendixTable5.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(ln_mean_peer_ability ln_mean_2ndorder_nbr_ability) sortvar(ln_mean_peer_ability ln_mean_2ndorder_nbr_ability) append						
				
		restore
		
		     

	*Appendix T6
		*attendance and plucking analysis
		reghdfe attend ln_mean_peer_ability, a(id dt_loc_num_alt) cluster(cycle_day_id_alt dt_loc_num_alt), if day_of_cycle_alt!=1 & draw==0
		outreg2 ln_mean_peer_ability using "${tables}/AppendixTable6.xls", adjr2 dec(3) fmt(fc) nocons excel ///
			keep(ln_mean_peer_ability) replace
		make_bayes_SEs, tablename(AT4 col1) outcome(attend) regressors(ln_mean_peer_ability) absorb(id dt_loc_num_alt) cluster(cycle_day_id_alt dt_loc_num_alt) sample(noday1) draws(10)		
			

		reghdfe plucking ln_mean_peer_ability, a(id dt_loc_num_alt) cluster(cycle_day_id_alt dt_loc_num_alt), if day_of_cycle_alt!=1 & draw==0
		outreg2 ln_mean_peer_ability using "${tables}/AppendixTable6.xls", adjr2 dec(3) fmt(fc) nocons excel ///
			keep(ln_mean_peer_ability) append
		make_bayes_SEs, tablename(AT4 col2) outcome(plucking) regressors(ln_mean_peer_ability) absorb(id dt_loc_num_alt) cluster(cycle_day_id_alt dt_loc_num_alt) sample(noday1) draws(10)		

	*Appendix T7
	*main peer effects estimates without double LOO correction
		reghdfe ln_kg ln_unadj_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
		outreg2 ln_unadj_mean_peer_ability using "${tables}/AppendixTable7.xls", adjr2 dec(3) fmt(fc) nocons excel ///
			keep(ln_unadj_mean_peer_ability) sortvar(ln_unadj_mean_peer_ability) replace
		make_bayes_SEs, tablename(AT5 col1) outcome(ln_kg) regressors(ln_unadj_mean_peer_ability) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		
		
		reghdfe ln_kg ln_unadj_mean_peer_ability, a(id Date Location) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
		outreg2 ln_unadj_mean_peer_ability using "${tables}/AppendixTable7.xls", adjr2 dec(3) fmt(fc) nocons excel ///
			keep(ln_unadj_mean_peer_ability) sortvar(ln_unadj_mean_peer_ability) append
		make_bayes_SEs, tablename(AT5 col1) outcome(ln_kg) regressors(ln_unadj_mean_peer_ability) absorb(id Date Location) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)		
			


	*Appendix T8
	*Does female effect come from differences in other characteristics?
		reghdfe ln_kg i.female#c.ln_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
		outreg2 i.female#c.ln_mean_peer_ability using "${tables}/AppendixTable8.xls", adjr2 dec(3) fmt(fc) nocons excel ///
			keep(i.female#c.ln_mean_peer_ability) replace
			*our effects are all for women 
			
			lincom _b[1.female#c.ln_mean_peer_ability]-_b[0.female#c.ln_mean_peer_ability]	
		make_bayes_SEs, tablename(AT6 col1) outcome(ln_kg) regressors(i.female#c.ln_mean_peer_ability) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)
			
	*additional test - female difference doesn't come from differences in other attributes
		reghdfe ln_kg i.female#c.ln_mean_peer_ability i.age_quartile#c.ln_mean_peer_ability i.constant_ability_quartile#c.ln_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
		lincom _b[1.female#c.ln_mean_peer_ability] - _b[0.female#c.ln_mean_peer_ability]
		outreg2 i.female#c.ln_mean_peer_ability i.age_quartile#c.ln_mean_peer_ability i.constant_ability_quartile#c.ln_mean_peer_ability using "${tables}/AppendixTable8.xls", adjr2 dec(3) fmt(fc) nocons excel ///
			keep(i.female#c.ln_mean_peer_ability i.age_quartile#c.ln_mean_peer_ability i.constant_ability_quartile#c.ln_mean_peer_ability) append
		make_bayes_SEs, tablename(AT6 col2) outcome(ln_kg) regressors(i.female#c.ln_mean_peer_ability i.age_quartile#c.ln_mean_peer_ability i.constant_ability_quartile#c.ln_mean_peer_ability) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)
		
		

	*Appendix T9
	*Additional gender heterogeneity
		reghdfe ln_kg ln_f_mean_peer_ability ln_m_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
		outreg2 ln_f_mean_peer_ability ln_m_mean_peer_ability using "${tables}/AppendixTable9.xls", adjr2 dec(3) fmt(fc) nocons excel ///
			keep(ln_f_mean_peer_ability ln_m_mean_peer_ability) replace			
		

		
		reghdfe ln_kg i.female#c.ln_f_mean_peer_ability i.female#c.ln_m_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
		outreg2 i.female#c.ln_f_mean_peer_ability i.female#c.ln_m_mean_peer_ability using "${tables}/AppendixTable9.xls", adjr2 dec(3) fmt(fc) nocons excel ///
			keep(i.female#c.ln_f_mean_peer_ability i.female#c.ln_m_mean_peer_ability) append

		
				
			
	*Appendix T10
		*Mechanisms by Gender
		capture drop male
		gen male = 1-female
			
			
		*age
			reghdfe ln_kg 1.male#i.age_quartile#c.ln_mean_peer_ability 1.female#i.age_quartile#c.ln_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
			
			test 	(1.age_quartile#c.ln_mean_peer_ability#1.male=1.age_quartile#c.ln_mean_peer_ability#1.female) ///
					(2.age_quartile#c.ln_mean_peer_ability#1.male=2.age_quartile#c.ln_mean_peer_ability#1.female) ///
					(3.age_quartile#c.ln_mean_peer_ability#1.male=3.age_quartile#c.ln_mean_peer_ability#1.female) ///
					(4.age_quartile#c.ln_mean_peer_ability#1.male=4.age_quartile#c.ln_mean_peer_ability#1.female) 
			
			outreg2 ln_mean_peer_ability 1.male#i.age_quartile#c.ln_mean_peer_ability 1.female#i.age_quartile#c.ln_mean_peer_ability ///
				using "${tables}/AppendixTable10.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(1.male#i.age_quartile#c.ln_mean_peer_ability 1.female#i.age_quartile#c.ln_mean_peer_ability) replace					
			
		
		*ability
			reghdfe ln_kg 1.male#i.constant_ability_quartile#c.ln_mean_peer_ability 1.female#i.constant_ability_quartile#c.ln_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
			
			test 	(1.constant_ability_quartile#c.ln_mean_peer_ability#1.male=1.constant_ability_quartile#c.ln_mean_peer_ability#1.female) ///
					(2.constant_ability_quartile#c.ln_mean_peer_ability#1.male=2.constant_ability_quartile#c.ln_mean_peer_ability#1.female) ///
					(3.constant_ability_quartile#c.ln_mean_peer_ability#1.male=3.constant_ability_quartile#c.ln_mean_peer_ability#1.female) ///
					(4.constant_ability_quartile#c.ln_mean_peer_ability#1.male=4.constant_ability_quartile#c.ln_mean_peer_ability#1.female) 
			
			outreg2 ln_mean_peer_ability 1.male#i.constant_ability_quartile#c.ln_mean_peer_ability 1.female#i.constant_ability_quartile#c.ln_mean_peer_ability ///
				using "${tables}/AppendixTable10.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(1.male#i.constant_ability_quartile#c.ln_mean_peer_ability 1.female#i.constant_ability_quartile#c.ln_mean_peer_ability) append					

			
					
		*experience
			reghdfe ln_kg 1.male#i.experience_quartile#c.ln_mean_peer_ability 1.female#i.experience_quartile#c.ln_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
			
			test 	(1.experience_quartile#c.ln_mean_peer_ability#1.male=1.experience_quartile#c.ln_mean_peer_ability#1.female) ///
					(2.experience_quartile#c.ln_mean_peer_ability#1.male=2.experience_quartile#c.ln_mean_peer_ability#1.female) ///
					(3.experience_quartile#c.ln_mean_peer_ability#1.male=3.experience_quartile#c.ln_mean_peer_ability#1.female) ///
					(4.experience_quartile#c.ln_mean_peer_ability#1.male=4.experience_quartile#c.ln_mean_peer_ability#1.female) 
			
			outreg2 ln_mean_peer_ability 1.male#i.experience_quartile#c.ln_mean_peer_ability 1.female#i.experience_quartile#c.ln_mean_peer_ability ///
				using "${tables}/AppendixTable10.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(1.male#i.experience_quartile#c.ln_mean_peer_ability 1.female#i.experience_quartile#c.ln_mean_peer_ability) append					

				
		*new workers
			gen old_worker = 1-new_worker
			reghdfe ln_kg 1.male#1.old_worker#c.ln_mean_peer_ability 1.male#1.new_worker#c.ln_mean_peer_ability 1.female#1.old_worker#c.ln_mean_peer_ability 1.female#1.new_worker#c.ln_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
			
			test 	(1.old_worker#c.ln_mean_peer_ability#1.male=1.old_worker#c.ln_mean_peer_ability#1.female) ///
					(1.new_worker#c.ln_mean_peer_ability#1.male=1.new_worker#c.ln_mean_peer_ability#1.female) ///
			
			outreg2 ln_mean_peer_ability 1.male#1.old_worker#c.ln_mean_peer_ability 1.male#1.new_worker#c.ln_mean_peer_ability 1.female#1.old_worker#c.ln_mean_peer_ability 1.female#1.new_worker#c.ln_mean_peer_ability ///
				using "${tables}/AppendixTable10.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(1.male#1.old_worker#c.ln_mean_peer_ability 1.male#1.new_worker#c.ln_mean_peer_ability 1.female#1.old_worker#c.ln_mean_peer_ability 1.female#1.new_worker#c.ln_mean_peer_ability) append					
							
			
		*friends and non-friends
						
			reghdfe ln_kg 1.male#c.zln_fr0_neighbor_ability_avg 1.male#1.fr0_neighbor_ability_none 1.male#c.zln_fr123_neighbor_ability_avg 1.male#1.fr123_neighbor_ability_none 1.female#c.zln_fr0_neighbor_ability_avg 1.female#1.fr0_neighbor_ability_none 1.female#c.zln_fr123_neighbor_ability_avg 1.female#1.fr123_neighbor_ability_none, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
			
			test 	(1.male#c.zln_fr0_neighbor_ability_avg=1.female#c.zln_fr0_neighbor_ability_avg) ///
					(1.male#1.fr0_neighbor_ability_none=1.female#1.fr0_neighbor_ability_none) ///
					(1.male#c.zln_fr123_neighbor_ability_avg=1.female#c.zln_fr123_neighbor_ability_avg) ///
					(1.male#1.fr123_neighbor_ability_none=1.female#1.fr123_neighbor_ability_none)
					
			
			outreg2 ln_mean_peer_ability 1.male#c.zln_fr0_neighbor_ability_avg 1.male#1.fr0_neighbor_ability_none 1.male#c.zln_fr123_neighbor_ability_avg 1.male#1.fr123_neighbor_ability_none 1.female#c.zln_fr0_neighbor_ability_avg 1.female#1.fr0_neighbor_ability_none 1.female#c.zln_fr123_neighbor_ability_avg 1.female#1.fr123_neighbor_ability_none ///
				using "${tables}/AppendixTable10.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(1.male#c.zln_fr0_neighbor_ability_avg 1.male#1.fr0_neighbor_ability_none 1.male#c.zln_fr123_neighbor_ability_avg 1.male#1.fr123_neighbor_ability_none 1.female#c.zln_fr0_neighbor_ability_avg 1.female#1.fr0_neighbor_ability_none 1.female#c.zln_fr123_neighbor_ability_avg 1.female#1.fr123_neighbor_ability_none) append					
						
	
		*lags
			reghdfe ln_kg 1.male#c.ln_mean_peer_ability 1.male#c.lag1_ln_mean_peer_ability 1.male#c.lag2_ln_mean_peer_ability 1.male#c.lag3_ln_mean_peer_ability 1.female#c.ln_mean_peer_ability 1.female#c.lag1_ln_mean_peer_ability 1.female#c.lag2_ln_mean_peer_ability 1.female#c.lag3_ln_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
			
			test 	(1.male#c.ln_mean_peer_ability=1.female#c.ln_mean_peer_ability) ///
					(1.male#c.lag1_ln_mean_peer_ability=1.female#c.lag1_ln_mean_peer_ability) ///
					(1.male#c.lag2_ln_mean_peer_ability=1.female#c.lag2_ln_mean_peer_ability) ///
					(1.male#c.lag3_ln_mean_peer_ability=1.female#c.lag3_ln_mean_peer_ability)
					
			
			outreg2 ln_mean_peer_ability 1.male#c.ln_mean_peer_ability 1.male#c.lag1_ln_mean_peer_ability 1.male#c.lag2_ln_mean_peer_ability 1.male#c.lag3_ln_mean_peer_ability 1.female#c.ln_mean_peer_ability 1.female#c.lag1_ln_mean_peer_ability 1.female#c.lag2_ln_mean_peer_ability 1.female#c.lag3_ln_mean_peer_ability ///
				using "${tables}/AppendixTable10.xls", adjr2 dec(3) fmt(fc) nocons excel ///
				keep(1.male#c.ln_mean_peer_ability 1.male#c.lag1_ln_mean_peer_ability 1.male#c.lag2_ln_mean_peer_ability 1.male#c.lag3_ln_mean_peer_ability 1.female#c.ln_mean_peer_ability 1.female#c.lag1_ln_mean_peer_ability 1.female#c.lag2_ln_mean_peer_ability 1.female#c.lag3_ln_mean_peer_ability) append					
				

				
			
			
				
	*Appendix T11
		*ability distribution by gender
		preserve
			keep if draw==0
			keep id constant_own_ability female
			drop if mi(constant_own_ability,female)
			duplicates drop
			isid id
			
			tabstat constant_own_ability, statistics(mean sd p10 p25 p50 p75 p90 N) by(female)
			
			bysort female: estpost sum ln_fr0_neighbor_ability_avg ln_fr123_neighbor_ability_avg, d
			 
			esttab using "${tables}/AppendixTable11.csv", cells("mean sd p10 p25 p50 p75 p90 count") replace					
		restore		
		
	
	*Appendix T12
		*preferences by gender		
		*see results under Table 8 above
			
	*Appendix T13
		*Test for assymmetry of responses to peers above and below own ability level
		gen peers_above = ln_own_ability < ln_mean_peer_ability
		gen ln_abil_diff = ln(abs(exp(ln_own_ability)-exp(ln_mean_peer_ability)))
		gen ln_abil_diff_X_peers_above = ln_abil_diff*peers_above
		gen ln_abil_diff_X_peers_below = ln_abil_diff*(1-peers_above)
		
		reghdfe ln_kg ln_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
		outreg2 ln_mean_peer_ability using "${tables}/AppendixTable13.xls", adjr2 dec(3) fmt(fc) nocons excel ///
			keep(ln_mean_peer_ability) sortvar(ln_mean_peer_ability) replace
		make_bayes_SEs, tablename(AT8 col1) outcome(ln_kg) regressors(ln_mean_peer_ability) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)
			
		reghdfe ln_kg ln_abil_diff_X_peers_above ln_abil_diff_X_peers_below peers_above, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
		outreg2 ln_abil_diff_X_peers_above ln_abil_diff_X_peers_below peers_above using "${tables}/AppendixTable13.xls", adjr2 dec(3) fmt(fc) nocons excel ///
			keep(ln_abil_diff_X_peers_above ln_abil_diff_X_peers_below peers_above) append
		make_bayes_SEs, tablename(AT8 col2) outcome(ln_kg) regressors(ln_abil_diff_X_peers_above ln_abil_diff_X_peers_below peers_above) absorb(id dt_loc_num) cluster(cycle_day_id dt_loc_num) sample(noday1) draws(10)
			
	
		
	*Appendix T14
		*distribution of mean peer ability by friends and nonfriends
		preserve
			reghdfe ln_kg ln_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
			keep if e(sample)
			
			gen ln_fr0_neighbor_ability_avg = zln_fr0_neighbor_ability_avg
			replace ln_fr0_neighbor_ability_avg = . if fr0_neighbor_ability_any==0
			gen fr0_neighbor_ability_avg = exp(ln_fr0_neighbor_ability_avg)
			
			gen ln_fr123_neighbor_ability_avg = zln_fr123_neighbor_ability_avg
			replace ln_fr123_neighbor_ability_avg = . if fr123_neighbor_ability_any==0
			gen fr123_neighbor_ability_avg = exp(ln_fr123_neighbor_ability_avg)

			estpost sum fr0_neighbor_ability_avg fr123_neighbor_ability_avg, d
			 
			esttab using "${tables}/AppendixTable14.csv", cells("mean sd p10 p25 p50 p75 p90 count") replace				
		restore		
		
		

*Paper Figures
	
	*Figure 1
		*Built manually in MS Office
		
		
	*Figure 2
		*kernel densities of ability and log ability
		preserve
			set scheme s2mono
			keep if draw==0
			keep id constant_own_ability
			drop if mi(constant_own_ability)
			duplicates drop
			isid id
			
			gen ln_constant_own_ability = ln(constant_own_ability)
			kdensity constant_own_ability, ///
				title("Kernel Density of Worker Ability") ///
				xlabel(`xlabels', noticks) xtitle("Worker Ability") ///
				ytitle("Density", orientation(horizontal)) ylabel(,angle(0)) ///
				graphregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white)) ///
				plotregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white))
			graph export "${tables}/Figure2A.pdf", as(pdf) replace
			graph export "${tables}/Figure2A.png", as(png) replace
			
			kdensity ln_constant_own_ability, ///
				title("Kernel Density of log(Worker Ability)") ///
				xlabel(`xlabels', noticks) xtitle("log(Worker Ability)") ///
				ytitle("Density", orientation(horizontal)) ylabel(,angle(0)) ///
				graphregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white)) ///
				plotregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white))
			graph export "${tables}/Figure2B.pdf", as(pdf) replace	
			graph export "${tables}/Figure2B.png", as(png) replace	
						
			sum constant_own_ability, d
			
			sum ln_constant_own_ability
			ksmirnov ln_constant_own_ability = normal((ln_constant_own_ability-r(mean))/r(sd))
			sktest ln_constant_own_ability

		restore		

	*Figure 3
		*binned scatterplot of log otuput vs log mean peer ability
		set scheme s2mono
		
		
		binscatter ln_kg ln_mean_peer_ability if day_of_cycle != 1 & draw==0, absorb(dt_loc_num) controls(i.id) ///
			xlabel(`xlabels', noticks) xtitle("log(Mean Peer Ability)") ///
			ytitle("log(Output)", orientation(horizontal)) ylabel(,angle(0)) ///
			graphregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white)) ///
			plotregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white))
		graph export "${tables}/Figure3.pdf", as(pdf) replace
		graph export "${tables}/Figure3.png", as(png) replace
			
			
		
	*Figure 4
		*kernel density of worker ability by gender
		preserve
			set scheme s2mono
			keep if draw==0
			keep id constant_own_ability female
			drop if mi(constant_own_ability,female)
			duplicates drop
			isid id
			
	
			
			twoway (kdensity constant_own_ability if female==1) (kdensity constant_own_ability if female==0), ///
				title("Kernel Density of log(Worker Ability) by Gender") ///
				xlabel(`xlabels', noticks) xtitle("log(Worker Ability)") ///
				ytitle("Density", orientation(horizontal)) ylabel(,angle(0)) ///
				legend(label(1 "Female") label(2 "Male")) ///
				graphregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white)) ///
				plotregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white))
			graph export "${tables}/Figure4.pdf", as(pdf) replace		
			graph export "${tables}/Figure4.png", as(png) replace		
			
			sum constant_own_ability, d
			bysort female: sum constant_own_ability, d

		restore				
		
				
		
		
	*Figure 5
		*graph of lag and lead results
		reghdfe ln_kg lag2_ln_mean_peer_ability lag1_ln_mean_peer_ability ln_mean_peer_ability lead1_ln_mean_peer_ability lead2_ln_mean_peer_ability, a(id dt_loc_num) cluster(cycle_day_id dt_loc_num), if day_of_cycle!=1 & draw==0
		test lag2_ln_mean_peer_ability lag1_ln_mean_peer_ability lead1_ln_mean_peer_ability lead2_ln_mean_peer_ability
		test lag2_ln_mean_peer_ability lag1_ln_mean_peer_ability ln_mean_peer_ability lead1_ln_mean_peer_ability lead2_ln_mean_peer_ability
		
		*make a graph of our results
		preserve
			*set scheme s2mono
		
		
			keep if _n<=5
			gen t = _n-3
			keep t
			
			
			gen beta = .
			replace beta = _b[lag2_ln_mean_peer_ability] if t==-2
			replace beta = _b[lag1_ln_mean_peer_ability] if t==-1
			replace beta = _b[ln_mean_peer_ability] if t==0
			replace beta = _b[lead1_ln_mean_peer_ability] if t==1
			replace beta = _b[lead2_ln_mean_peer_ability] if t==2

			gen se = .			
			replace se = _se[lag2_ln_mean_peer_ability] if t==-2
			replace se = _se[lag1_ln_mean_peer_ability] if t==-1
			replace se = _se[ln_mean_peer_ability] if t==0
			replace se = _se[lead1_ln_mean_peer_ability] if t==1
			replace se = _se[lead2_ln_mean_peer_ability] if t==2		
			
			gen df = e(df_r)
			
			gen citop = beta + invttail(df,0.025)*se
			gen cibot = beta - invttail(df,0.025)*se
			
			
			
			twoway 	(scatter beta t, msymbol(square) msize(small) mcolor(black) connect(l) lcolor(black)) ///
					(scatter citop t, msymbol(square) msize(small)  mcolor(black) connect(l) lcolor(black) lpattern(dot)) ///
					(scatter cibot t, msymbol(square) msize(small)  mcolor(black) connect(l) lcolor(black) lpattern(dot)), ///
					yline(0, lcolor(black)) legend(order(1 "Point Estimate" 2 "95% CI") cols(1)) ///
					xsize(7) ysize(5) ///
					ylabel(,angle(0)) ///
					xtitle("Time Period (0 = Current Day)") ytitle("Estimated" "Peer Effect", orientation(horizontal) margin(medsmall)) ///
					graphregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white)) ///
					plotregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white))	
			graph export "${tables}/Figure5.pdf", as(pdf) replace		
			graph export "${tables}/Figure5.png", as(png) replace				
		
		restore
		

*Additional analyses

	*length of cycle 

		preserve

			drop if mi(day_of_cycle)
			collapse (max) cycle_length = day_of_cycle, by(id)
			
			sum cycle_length, d
			
			tab cycle_length, m

		restore

		
		clear all

	*Silver method



		use "${JHRdir}\adjacency_data.dta", clear

		*make string that uniquely identifies all combinations of peers
		egen peerlist = concat(is_*_neighbor)

		*add on worker ID - uniquely identifies all peer groups, including self
		tostring id, gen(_id) format(%003.0f)
		gen dc_gc = _id+peerlist



		capture drop *_fe*
		capture drop split
		capture drop _merge*



		reghdfe ln_kg, a(id dt_loc_num dc_gc, savefe) cluster(cycle_day_id dt_loc_num)


		rename __hdfe1__ worker_fe
		rename __hdfe2__ fielddate_fe
		rename __hdfe3__ group_fe

		preserve

			keep if e(sample)
			*overall variance in output
			sum ln_kg, d
			local var_overall = r(Var)
			sum worker_fe, d
			local var_worker = r(Var)
			sum fielddate_fe, d
			local var_fielddate = r(Var)
			sum group_fe, d
			local var_group = r(Var)
			
			local SD_overall = sqrt(`var_overall')
			local SD_fielddate = sqrt(`var_fielddate')
			local SD_worker = sqrt(`var_worker')
			local SD_group = sqrt(`var_group')
			
			
			local R2_overall = (`var_worker' + `var_fielddate'+`var_group')/`var_overall'	
			local R2_fielddate = (`var_fielddate')/`var_overall'	
			local R2_worker = (`var_worker')/`var_overall'	
			local R2_group = (`var_group')/`var_overall'	

			corr worker_fe_1 worker_fe_0, cov
			local SS_var_worker =`r(cov_12)'
			corr worker_fe_1 worker_fe_0
			local SS_SD_worker = `r(rho)'
			local SS_R2_worker =(`SS_var_worker')/`var_overall'	
			
			corr group_fe_1 group_fe_0, cov
			local SS_var_group =`r(cov_12)'
			corr group_fe_1 group_fe_0
			local SS_SD_group = `r(rho)'
			local SS_R2_group =(`SS_var_group')/`var_overall'		
			
		restore


		copy "${JHRdir}\dofiles\Silver method Results Template.xlsx" "${tables}\Silver method Results $S_DATE.xlsx", replace

		putexcel set "${tables}\Silver method Results $S_DATE.xlsx", modify




		*build matrix with silver results and our results

		matrix Silv = J(4,3,.)
		local vars overall fielddate worker group
		local i = 1
		foreach var of local vars{

			local stats var SD R2
				local j = 1
				foreach stat of local stats{
					matrix Silv[`i',`j']=``stat'_`var''
					local j = `j'+1
				}
			

			local i = `i'+1
		}

		matrix list Silv


		*output results to excel
		putexcel B3 = matrix(Silv)


			
			